﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace GMS.BIZ
{
    public class Companys
    {
        private string studentID;

        /// <summary>
        /// 初始化Companys
        /// </summary>
        /// <param name="studentID">学生学号</param>
        public Companys(string studentID)
        {
            this.studentID = studentID;
        }

        /// <summary>
        /// 数该学生有多少条工作单位的记录
        /// </summary>
        public int Count()
        {
            GMS.ORM.DAL.GCompany optGCompany = new GMS.ORM.DAL.GCompany();
            DataSet ds = optGCompany.GetList("std_id='" + this.studentID + "' ORDER BY id");
            try
            {
                return ds.Tables[0].Rows.Count;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 添加一条学生工作单位
        /// </summary>
        /// <param name="inputCompany">工作单位的对象</param>
        public string Add(Company inputCompany)
        {
            GMS.ORM.Model.GCompany myGCompany = new GMS.ORM.Model.GCompany();
            myGCompany.std_id = this.studentID;
            myGCompany.com_name = inputCompany.com_name;
            myGCompany.com_style = inputCompany.com_style;
            myGCompany.com_address = inputCompany.com_address;
            myGCompany.com_tel = inputCompany.com_tel;
            myGCompany.com_post = inputCompany.com_post;
            myGCompany.com_web = inputCompany.com_web;
            myGCompany.ifSLD = inputCompany.ifSLD;
            
            GMS.ORM.DAL.GCompany optGCompany = new GMS.ORM.DAL.GCompany();
            bool ok = optGCompany.Add(myGCompany);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 删除一条学生工作单位
        /// </summary>
        /// <param name="id">工作单位的id号</param>
        public string Delete(decimal id)
        {
            GMS.ORM.DAL.GCompany optGCompany = new GMS.ORM.DAL.GCompany();
            bool ok = optGCompany.Delete(id);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 修改一条学生工作单位
        /// </summary>
        /// <param name="inputCompany">工作单位的对象</param>
        public string Update(Company inputCompany)
        {
            GMS.ORM.Model.GCompany myGCompany = new GMS.ORM.Model.GCompany();
            myGCompany.id = inputCompany.id;
            myGCompany.std_id = this.studentID;
            myGCompany.com_name = inputCompany.com_name;
            myGCompany.com_style = inputCompany.com_style;
            myGCompany.com_address = inputCompany.com_address;
            myGCompany.com_tel = inputCompany.com_tel;
            myGCompany.com_post = inputCompany.com_post;
            myGCompany.com_web = inputCompany.com_web;
            myGCompany.ifSLD = inputCompany.ifSLD;

            GMS.ORM.DAL.GCompany optGCompany = new GMS.ORM.DAL.GCompany();
            bool ok = optGCompany.Update(myGCompany);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 获得一条学生的工作单位
        /// </summary>
        /// <param name="id">id</param>
        public Company GetOne(decimal id)
        {
            Company outputCompany = new Company();
            GMS.ORM.Model.GCompany myGCompany = new GMS.ORM.Model.GCompany();
            GMS.ORM.DAL.GCompany optGCompany = new GMS.ORM.DAL.GCompany();
            try
            {
                myGCompany = optGCompany.GetModel(id);
                outputCompany.id = myGCompany.id;
                outputCompany.std_id = myGCompany.std_id;
                outputCompany.com_name = myGCompany.com_name;
                outputCompany.com_style = myGCompany.com_style;
                outputCompany.com_address = myGCompany.com_address;
                outputCompany.com_tel = myGCompany.com_tel;
                outputCompany.com_post = myGCompany.com_post;
                outputCompany.com_web = myGCompany.com_web;
                outputCompany.ifSLD = myGCompany.ifSLD;
                return outputCompany;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 获得一个学生的英语能力的数组
        /// </summary>
        public ArrayList GetList()
        {
            ArrayList returnList = new ArrayList();

            GMS.ORM.DAL.GCompany optGCompany = new GMS.ORM.DAL.GCompany();
            DataSet ds = optGCompany.GetList("std_id='" + this.studentID + "' ORDER BY id");
            if(ds.Tables[0].Rows.Count==0)
                return returnList;
            else
            {
                int howMany=ds.Tables[0].Rows.Count;
                for(int i=0;i<howMany;i++)
                    returnList.Add(decimal.Parse(ds.Tables[0].Rows[i]["id"].ToString()));
                return returnList;
            }
        }

        /// <summary>
        /// 获得一个学生的英语能力的DataSet
        /// </summary>
        public DataSet GetDataSet()
        {
            GMS.ORM.DAL.GCompany optGCompany=new GMS.ORM.DAL.GCompany();
            DataSet ds= optGCompany.GetList("std_id='"+this.studentID+"' ORDER BY id");
            return ds;
        }

        /// <summary>
        /// 返回工作单位信息：id,std_id,com_name,com_style,com_address,com_tel,com_post,com_web,ifSLD
        /// </summary>
        /// <param name="studentID">学生学号，可以为空</param>
        /// <param name="companyStyle">公司类型，可以为空</param>
        /// <param name="companyName">公司名字，可以为空</param>
        static public DataSet GetDataSet(string studentID, int? companyStyle, string companyName, bool? ifSLD)
        {
            string queryString = "1=1";
            if (studentID != string.Empty) queryString = queryString + " AND std_id='" + studentID + "'";
            if (companyStyle != null && companyStyle != 0) queryString = queryString + " AND com_style=" + companyStyle.ToString();
            if (companyName != string.Empty) queryString = queryString + " AND com_name='" + companyName + "'";
            if (ifSLD == true) queryString = queryString + " AND ifSLD=1";
            else if (ifSLD == false) queryString = queryString + " AND ifSLD=0";

            GMS.ORM.DAL.GCompany optGCompany = new GMS.ORM.DAL.GCompany();
            return optGCompany.GetList(queryString + " ORDER BY id");
        }
    }
}
